/**
 * 格式化日期
 * date 传入的日期格式
 * fmt 所需日期格式 'yyyy-MM-dd hh:mm:ss
 * @param date
 * @param fmt
 * @returns {*}
 */
function formatDate (date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length))
    }
  }
  return fmt
}

/**
 * 获取10天的日期 及 星期
 * date:表示日期 [Sting]  eg: '2018-01-24'
 * week:表示星期 [Sting] eg: '星期一
 * @returns {Array}
 */
function getBeforeTenDate () {
  let _ten = []
  for (var i = 0; i < 10; i++) {
    let now = new Date()
    let _obj = {}
    now.setDate(now.getDate() - i)
    _obj.date = formatDate(now, 'yyyy-MM-dd')
    switch (now.getDay()) {
      case 0:
        _obj.week = '星期日'
        break
      case 1:
        _obj.week = '星期一'
        break
      case 2:
        _obj.week = '星期二'
        break
      case 3:
        _obj.week = '星期三'
        break
      case 4:
        _obj.week = '星期四'
        break
      case 5:
        _obj.week = '星期五'
        break
      case 6:
        _obj.week = '星期六'
        break
    }
    _ten.push(_obj)
  }
  return _ten
}

export {
  formatDate,
  getBeforeTenDate
}
